package com.yadang.xml;


import com.yadang.xml.entity.AccountReport;
import com.yadang.xml.entity.Record;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;
import java.io.StringReader;
import java.util.List;

/**
 * @author: 亚当
 * @date: 2023年03月21日 0:29
 * @package: com.yadang.xml
 * @Description: TODO
 */
public class XmlUtils {


    public static void main(String[] args) throws Exception {
        xmlToList();
        String path = "D:\\IDEA_Workspaces\\YadangDayDayUp\\baseKnowledge\\src\\main\\java\\com\\yadang\\xml\\2.xml";

        String bond_acct = getNodeName(path, "BOND_ACCT");
        System.out.println(bond_acct);

        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Msg>\n" +
                "    <Header>\n" +
                "        <Vrsn>\n" +
                "            <VrsnID>1.0</VrsnID>\n" +
                "        </Vrsn>\n" +
                "        <Sndr>\n" +
                "            <SndrId>CCDC</SndrId>\n" +
                "        </Sndr>\n" +
                "        <Rcvr>\n" +
                "            <RcvrId>DCSM</RcvrId>\n" +
                "            <RcvrClntId>0000000012</RcvrClntId>\n" +
                "        </Rcvr>\n" +
                "        <FileDesc>\n" +
                "            <FileSndDateTime>2023-03-20 02:26:15</FileSndDateTime>\n" +
                "            <FileType>xml</FileType>\n" +
                "            <FileName>托管账户总对账报告（细分科目）资产托管_20230320022615.xml</FileName>\n" +
                "        </FileDesc>\n" +
                "    </Header>\n" +
                "    <Body>\n" +
                "        <Record>\n" +
                "            <BIZ_DATE>2023-03-19</BIZ_DATE>\n" +
                "            <BOND_ACCT>00000012647</BOND_ACCT>\n" +
                "            <BOND_ACCT_NAME_ABBR>矧猎洱袜并啮腰目卫</BOND_ACCT_NAME_ABBR>\n" +
                "            <BOND_CODE>180017</BOND_CODE>\n" +
                "            <BOND_NAME_ABBR>85募息钵草63</BOND_NAME_ABBR>\n" +
                "            <DENOMINATED_CURRENCY_DESC>人民币</DENOMINATED_CURRENCY_DESC>\n" +
                "            <SUBJ_SET>\n" +
                "                <BOND_SUBJ_NAME>可用</BOND_SUBJ_NAME>\n" +
                "                <SUBJ_FACE_AMNT>1000</SUBJ_FACE_AMNT>\n" +
                "                <SUBJ_BAL_UNIT_DESC>万元</SUBJ_BAL_UNIT_DESC>\n" +
                "                <BOND_SUBJ_NAME>质权</BOND_SUBJ_NAME>\n" +
                "                <SUBJ_FACE_AMNT>5000</SUBJ_FACE_AMNT>\n" +
                "                <SUBJ_BAL_UNIT_DESC>万元</SUBJ_BAL_UNIT_DESC>\n" +
                "            </SUBJ_SET>\n" +
                "        </Record>\n" +
                "    </Body>\n" +
                "</Msg>\n";
        String bondAcct = getNodeNameByStr(xml, "BOND_ACCT");
        System.out.println(bondAcct);
    }

    // 获取第一个Node的值
    public static String getNodeName(String path, String nodeName) throws ParserConfigurationException, SAXException, IOException {
        DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = builderFactory.newDocumentBuilder();
        Document doc = builder.parse(new File(path));
        NodeList bizDate = doc.getElementsByTagName(nodeName);  // 获取所有<BIZ_DATE>节点
        String textContent = bizDate.item(0).getTextContent(); // 获取第一个值
        return textContent;
    }

    public static String getNodeNameByStr(String xml, String nodeName) throws Exception {
        if ("" == xml || xml == null){
            return null;
        }
        if ("" == nodeName || xml == nodeName){
            return null;
        }
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource source = new InputSource(new StringReader(xml)); // 这里假设XML数据存放在xmlString变量中
        Document doc = builder.parse(source);

        NodeList nodeList = doc.getElementsByTagName(nodeName); // 获取所有<BIZ_DATE>节点
        String bizDateValue = nodeList.item(0).getTextContent(); // 获取第一个<BIZ_DATE>节点的文本值
        return bizDateValue;
    }
    private static void xmlToList() {
        String xmlData = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
                "<Msg>\n" +
                "    <Header>\n" +
                "        <Vrsn>\n" +
                "            <VrsnID>1.0</VrsnID>\n" +
                "        </Vrsn>\n" +
                "        <Sndr>\n" +
                "            <SndrId>CCDC</SndrId>\n" +
                "        </Sndr>\n" +
                "        <Rcvr>\n" +
                "            <RcvrId>DCSM</RcvrId>\n" +
                "            <RcvrClntId>0000000012</RcvrClntId>\n" +
                "        </Rcvr>\n" +
                "        <FileDesc>\n" +
                "            <FileSndDateTime>2023-03-20 02:26:15</FileSndDateTime>\n" +
                "            <FileType>xml</FileType>\n" +
                "            <FileName>托管账户总对账报告（细分科目）资产托管_20230320022615.xml</FileName>\n" +
                "        </FileDesc>\n" +
                "    </Header>\n" +
                "    <Body>\n" +
                "        <Record>\n" +
                "            <BIZ_DATE>2023-03-19</BIZ_DATE>\n" +
                "            <BOND_ACCT>00000012647</BOND_ACCT>\n" +
                "            <BOND_ACCT_NAME_ABBR>矧猎洱袜并啮腰目卫</BOND_ACCT_NAME_ABBR>\n" +
                "            <BOND_CODE>180017</BOND_CODE>\n" +
                "            <BOND_NAME_ABBR>85募息钵草63</BOND_NAME_ABBR>\n" +
                "            <DENOMINATED_CURRENCY_DESC>人民币</DENOMINATED_CURRENCY_DESC>\n" +
                "            <SUBJ_SET>\n" +
                "                <BOND_SUBJ_NAME>可用</BOND_SUBJ_NAME>\n" +
                "                <SUBJ_FACE_AMNT>0</SUBJ_FACE_AMNT>\n" +
                "                <SUBJ_BAL_UNIT_DESC>万元</SUBJ_BAL_UNIT_DESC>\n" +
                "                <BOND_SUBJ_NAME>质权</BOND_SUBJ_NAME>\n" +
                "                <SUBJ_FACE_AMNT>0</SUBJ_FACE_AMNT>\n" +
                "                <SUBJ_BAL_UNIT_DESC>万元</SUBJ_BAL_UNIT_DESC>\n" +
                "            </SUBJ_SET>\n" +
                "        </Record>\n" +
                "        <Record>\n" +
                "            <BIZ_DATE>2023-03-19</BIZ_DATE>\n" +
                "            <BOND_ACCT>00000050731</BOND_ACCT>\n" +
                "            <BOND_ACCT_NAME_ABBR>建士裕慑鲍苏糍亨锻妤定翅糍</BOND_ACCT_NAME_ABBR>\n" +
                "            <BOND_CODE>130240</BOND_CODE>\n" +
                "            <BOND_NAME_ABBR>35圏播88</BOND_NAME_ABBR>\n" +
                "            <DENOMINATED_CURRENCY_DESC>人民币</DENOMINATED_CURRENCY_DESC>\n" +
                "            <SUBJ_SET>\n" +
                "                <BOND_SUBJ_NAME>质权</BOND_SUBJ_NAME>\n" +
                "                <SUBJ_FACE_AMNT>0</SUBJ_FACE_AMNT>\n" +
                "                <SUBJ_BAL_UNIT_DESC>万元</SUBJ_BAL_UNIT_DESC>\n" +
                "                <BOND_SUBJ_NAME>可用</BOND_SUBJ_NAME>\n" +
                "                <SUBJ_FACE_AMNT>10000</SUBJ_FACE_AMNT>\n" +
                "                <SUBJ_BAL_UNIT_DESC>万元</SUBJ_BAL_UNIT_DESC>\n" +
                "            </SUBJ_SET>\n" +
                "        </Record>\n" +
                "        <Record>\n" +
                "            <BIZ_DATE>2023-03-19</BIZ_DATE>\n" +
                "            <BOND_ACCT>00000016393</BOND_ACCT>\n" +
                "            <BOND_ACCT_NAME_ABBR>滨福氰敷羼材苯敷</BOND_ACCT_NAME_ABBR>\n" +
                "            <BOND_CODE>1828006</BOND_CODE>\n" +
                "            <BOND_NAME_ABBR>85锘条车换矮措31</BOND_NAME_ABBR>\n" +
                "            <DENOMINATED_CURRENCY_DESC>人民币</DENOMINATED_CURRENCY_DESC>\n" +
                "            <SUBJ_SET>\n" +
                "                <BOND_SUBJ_NAME>质权</BOND_SUBJ_NAME>\n" +
                "                <SUBJ_FACE_AMNT>0</SUBJ_FACE_AMNT>\n" +
                "                <SUBJ_BAL_UNIT_DESC>万元</SUBJ_BAL_UNIT_DESC>\n" +
                "            </SUBJ_SET>\n" +
                "        </Record>\n" +
                "        <Record>\n" +
                "            <BIZ_DATE>2023-03-19</BIZ_DATE>\n" +
                "            <BOND_ACCT>00000036568</BOND_ACCT>\n" +
                "            <BOND_ACCT_NAME_ABBR>赔活咸拖3伶w铆</BOND_ACCT_NAME_ABBR>\n" +
                "            <BOND_CODE>200017</BOND_CODE>\n" +
                "            <BOND_NAME_ABBR>71集息癌铸46</BOND_NAME_ABBR>\n" +
                "            <DENOMINATED_CURRENCY_DESC>人民币</DENOMINATED_CURRENCY_DESC>\n" +
                "            <SUBJ_SET>\n" +
                "                <BOND_SUBJ_NAME>质权</BOND_SUBJ_NAME>\n" +
                "                <SUBJ_FACE_AMNT>0</SUBJ_FACE_AMNT>\n" +
                "                <SUBJ_BAL_UNIT_DESC>万元</SUBJ_BAL_UNIT_DESC>\n" +
                "                <BOND_SUBJ_NAME>可用</BOND_SUBJ_NAME>\n" +
                "                <SUBJ_FACE_AMNT>0</SUBJ_FACE_AMNT>\n" +
                "                <SUBJ_BAL_UNIT_DESC>万元</SUBJ_BAL_UNIT_DESC>\n" +
                "            </SUBJ_SET>\n" +
                "        </Record>\n" +
                "    </Body>\n" +
                "</Msg>\n";

        try {
            JAXBContext jaxbContext = JAXBContext.newInstance(AccountReport.class);
            Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

            StringReader reader = new StringReader(xmlData);
            AccountReport accountReport = (AccountReport) unmarshaller.unmarshal(reader);

            List<Record> records = accountReport.getRecords();
            System.out.println(records);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
